package com.xyw.code.study20200813.test;

import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xuyiwei
 * Date: 2020/8/28
 * Time: 下午4:47
 * Email: 1328312923@qq.com
 * Description: No Description
 **/
public class MyStringBuilder {

    char[] value;
    int length;

    //初始化

    public MyStringBuilder() {
        this.value = new char[16];
        this.length = 0;
    }

    public MyStringBuilder(String str) {
        this.value = new char[16];
        this.length = 0;
        if(!StringUtils.isEmpty(str)){
            this.value = str.toCharArray();
            this.length = str.length();
        }
    }

    @Override
    public String toString() {
        String str = new String(this.value);
        return str.toString();
    }

    public void append(String str){
        if(!StringUtils.isEmpty(str)){
            if(str.length()+length>value.length){
                if(str.length()+length<value.length*2){
                    //如果大了  则扩容
                    this.value = Arrays.copyOf(value,value.length*2);
                }else{
                    this.value = Arrays.copyOf(value,str.length()+length);
                }
                System.arraycopy(str.toCharArray(),0,value,length,str.length());
                this.length = str.toCharArray().length+length;
            }
        }

    }

    public static void main(String[] args) {
        MyStringBuilder stringBuilder = new MyStringBuilder();
        stringBuilder.append("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
        stringBuilder.append("vvvvv");
        System.out.println(stringBuilder.toString());

        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("aa");
    }
}
